package com.fa4j.system.account.service.impl;

import com.fa4j.common.base.exception.CommonRequestError;
import com.fa4j.common.base.model.RemoveCmd;
import com.fa4j.system.account.convert.AccountLogConverter;
import com.fa4j.system.account.model.AccountLog;
import com.fa4j.system.account.model.AccountLogCreateCmd;
import com.fa4j.system.account.repository.AccountLogRepository;
import com.fa4j.system.account.service.AccountLogService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

/**
 * 账户日志 服务接口实现
 *
 * @since 2025-07-15 17:42:53
 */
@Slf4j
@Service
@Validated
@RequiredArgsConstructor
public class AccountLogServiceImpl implements AccountLogService{

    private final AccountLogRepository accountLogRepository;
    private final AccountLogConverter accountLogConverter;

    /**
     * 新增账户日志
     *
     * @param cmd 新增参数
     * @return 账户日志
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AccountLog create(@Valid AccountLogCreateCmd cmd) {
        AccountLog entity = accountLogConverter.toEntity(cmd);
        accountLogRepository.create(entity);
        return entity;
    }


    /**
     * 删除账户日志
     *
     * @param cmd 删除参数
     * @return 账户日志
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AccountLog remove(@Valid RemoveCmd<Long> cmd) {
        AccountLog entity = accountLogRepository.findById(cmd.getId())
                .orElseThrow(() -> CommonRequestError.NOT_FOUND.build("账户日志"));
        entity.setOperatorId(cmd.getOperatorId());
        accountLogRepository.remove(entity);
        return entity;
    }

}




